<?php
/**
 * admin customer left menu
 *
 * @category   Mage
 * @package    Mage_Adminhtml
 * @author      Magento Core Team <core@magentocommerce.com>
 */
class Webkul_Marketplacepartner_Block_Adminhtml_Customer_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs
{
    public function __construct(){
        parent::__construct();
        $this->setId('customer_info_tabs');
        $this->setDestElementId('edit_form');
        $this->setTitle(Mage::helper('customer')->__('Customer Information'));
    }

    protected function _beforeToHtml(){
        $this->addTab('account', array(
            'label'     => Mage::helper('customer')->__('Account Information'),
            'content'   => $this->getLayout()->createBlock('adminhtml/customer_edit_tab_account')->initForm()->toHtml(),
            'active'    => Mage::registry('current_customer')->getId() ? false : true
        ));
        $this->addTab('addresses', array(
            'label'     => Mage::helper('customer')->__('Addresses'),
            'content'   => $this->getLayout()->createBlock('adminhtml/customer_edit_tab_addresses')->initForm()->toHtml(),
        ));
        // load: Orders, Shopping Cart, Wishlist, Product Reviews, Product Tags - with ajax
        if (Mage::registry('current_customer')->getId()) {
            if (Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/view')) {
                $this->addTab('orders', array(
                    'label'     => Mage::helper('customer')->__('Orders'),
                    'class'     => 'ajax',
                    'url'       => $this->getUrl('*/*/orders', array('_current' => true)),
                 ));
            }
            $this->addTab('cart', array(
                'label'     => Mage::helper('customer')->__('Shopping Cart'),
                'class'     => 'ajax',
                'url'       => $this->getUrl('*/*/carts', array('_current' => true)),
            ));
            $this->addTab('wishlist', array(
                'label'     => Mage::helper('customer')->__('Wishlist'),
                'class'     => 'ajax',
                'url'       => $this->getUrl('*/*/wishlist', array('_current' => true)),
            ));
            if (Mage::getSingleton('admin/session')->isAllowed('newsletter/subscriber')) {
                $this->addTab('newsletter', array(
                    'label'     => Mage::helper('customer')->__('Newsletter'),
                    'content'   => $this->getLayout()->createBlock('adminhtml/customer_edit_tab_newsletter')->initForm()->toHtml()
                ));
            }
            if (Mage::getSingleton('admin/session')->isAllowed('catalog/reviews_ratings')) {
                $this->addTab('reviews', array(
                    'label'     => Mage::helper('customer')->__('Product Reviews'),
                    'class'     => 'ajax',
                    'url'       => $this->getUrl('*/*/productReviews', array('_current' => true)),
                ));
            }
            if (Mage::getSingleton('admin/session')->isAllowed('catalog/tag')) {
                $this->addTab('tags', array(
                    'label'     => Mage::helper('customer')->__('Product Tags'),
                    'class'     => 'ajax',
                    'url'       => $this->getUrl('*/*/productTags', array('_current' => true)),
                ));
            }
			
				
			
			//if partner check
			$customerid=Mage::registry('current_customer')->getId();
			$isPartner= Mage::getModel('marketplaceprofile/marketplaceprofile')->isPartner($customerid);
			if($isPartner==1){
				$this->addTab('payment', array(
					'label'     => Mage::helper('customer')->__('Payment Mode'),
					'content'   => $this->paymentmode(),
				));
				$this->addTab('sellercommision', array(
					'label'     => Mage::helper('customer')->__("Seller's Commision"),
					'content'   => $this->sellercommision(),
				));	
				$this->addTab('newproduct', array(
					'label'     => Mage::helper('customer')->__("Add Product"),
					'content'   => $this->addproduct(),
				));
				$this->addTab('notpartner', array(
					'label'     => Mage::helper('customer')->__("Want to remove a Partner"),
					'content'   => $this->removepartner(),
				));
			}
			else {
			$this->addTab('partner', array(
					'label'     => Mage::helper('customer')->__('Want to be Partner'),
					'content'   => $this->wantpartner(),
				));
			}
			
        }
        $this->_updateActiveTab();
        Varien_Profiler::stop('customer/tabs');
        return parent::_beforeToHtml();
    }	
	protected function paymentmode(){		
		$id=Mage::registry('current_customer')->getId();
		$row =Mage::getModel('marketplaceprofile/marketplaceprofile')->querypaymentmode($id);
		if(strlen($row['paymentsource'])>0){	
			return '<div class="entry-edit">
						<div class="entry-edit-head">
							<h4 class="icon-head head-customer-view">Payment Details</h4>
						</div>
						<fieldset>
							<address>
								<strong>'.$row['paymentsource'].'</strong><br>
							</address>
						</fieldset>
					</div>';
		}
		else{
			return '<div class="entry-edit">
							<div class="entry-edit-head">
								<h4 class="icon-head head-customer-view">Payment Details</h4>
							</div>
							<fieldset>
								<address>
									<strong>Not Mentioned Yet</strong><br>
								</address>
							</fieldset>
						</div>';
		}
	
	}	
	protected function sellercommision(){		
		$mysqlprefix = Mage::getConfig()->getTablePrefix();//code for mysql prefix
		$write = Mage::getSingleton('core/resource')->getConnection('core_write');
		$id=Mage::registry('current_customer')->getId();			
		$readresultcom=$write->query("select * from  ".$mysqlprefix."marketplacepartner_entity_saleperpartner  where mageuserid =$id");
		$rowcom = $readresultcom->fetch();
		$readresultcomtotal=$write->query("select * from  ".$mysqlprefix."marketplacepartner_entity_saleslist  where mageproownerid =$id");
		$rowcomtotal = $readresultcomtotal->fetchAll();
		$tsale=0;
		$tcomm=0;
		$tact=0;
		$currencysign=Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())->getSymbol();
		foreach($rowcomtotal as $key){
			$tsale+=$key['totalamount'];
			$tcomm+=$key['totalcommision'];
			$tact+=$key['actualparterprocost'];
		}
		if($rowcom['commision']<1){$rowcom['commision']=0;}
		return '<div class="entry-edit">
					<div class="entry-edit-head">
						<h4 class="icon-head head-customer-view">Commision Details</h4>
					</div>
					<fieldset>
						Set Commision In Percentage For This Particular Partner : <input name="commision" type="text" classs="input-text no-changes" value="'.$rowcom['commision'].'"/>
						<table class="grid table" id="customer_cart_grid1_table" >
							<tr>
								<td>&nbsp;</td>
								<td>&nbsp;</td>
							</tr>
							<tr>
								<td>Total Sale</td>
								<td>'.Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())->getSymbol().' '.$tsale.'</td>
							</tr>
							<tr>
								<td>Total Partner Sale</td>
								<td>'.Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())->getSymbol().' '.$tact.'</td>
							</tr>
							<tr>
								<td>Total Admin Sale</td>
								<td>'.Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())->getSymbol().' '.$tcomm.'</td>
							</tr>
							<tr>
								<td>Current Commision %</td>
								<td>'.$rowcom['commision'].'%</td>
							</tr>
						</table>
					</fieldset>							
				</div>';
	}
	protected function addproduct(){		
		return '<div class="entry-edit">
					<div class="entry-edit-head">
						<h4 class="icon-head head-customer-view">Assign Product To Seller</h4>
					</div>
					<fieldset>
						Enter Product ID : <input name="sellerassignproid" type="text" classs="input-text no-changes" value=""/>	&nbsp;&nbsp;&nbsp;&nbsp;<b>Notice: Enter Only Integer value.</b>							
					</fieldset>							
				</div>';
	}
	
	protected function wantpartner(){		
		return '<div class="entry-edit">
					<div class="entry-edit-head">
						<h4 class="icon-head head-customer-view">Want to become a Partner</h4>
					</div>
					<fieldset>
						
						<input type="checkbox" name="partnertype" value="1">&nbsp;Is Partner
					</fieldset>							
				</div>';
	}
	
	protected function removepartner(){		
		return '<div class="entry-edit">
					<div class="entry-edit-head">
						<h4 class="icon-head head-customer-view">Want to remove Partner</h4>
					</div>
					<fieldset>
						<input type="checkbox" name="partnertype" value="2">&nbsp;Is not Partner
					</fieldset>							
				</div>';
	}
    protected function _updateActiveTab(){
        $tabId = $this->getRequest()->getParam('tab');
        if( $tabId ) {
            $tabId = preg_replace("#{$this->getId()}_#", '', $tabId);
            if($tabId) {
                $this->setActiveTab($tabId);
            }
        }
    }
}
